package com.fit.pages.card;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.annotations.AnonymousAccess;
import com.fit.annotations.CoachAccess;
import com.fit.annotations.MemberAccess;
import com.fit.dao.card.CardDAO;
import com.fit.entities.card.Card;
import com.fit.pages.ErrorPage;

@AnonymousAccess
@CoachAccess
@MemberAccess
public class Edit {

	@Property
	@Persist
	private Card card;
	@Inject
	private CardDAO cardDAO;
	@InjectPage
	private ErrorPage errorPage;
	@Property
	private double newPrice;

	Object onActivate(Long id) {
		card = cardDAO.findByID(id);
		if (card == null) {
			errorPage.setMessage("Error while editing card ");
			return errorPage;
		}
		return null;
	}

	Object onSuccess() {
		try {

			if (newPrice != -1) {
				
				if (newPrice == card.getPrice()) {
					card.setPrice(newPrice);
				}
			}
			cardDAO.update(card);
			return Browse.class;
		} catch (Exception e) {
			errorPage.setMessage("Error while updating changes");
			return errorPage;
		}

	}
}
